Repository navigation
[A11y] Connect the accessibility override controls to the app - #10065
victorsanni wants to merge 9 commits into
Conversation
Adds ViewMetricsOverrideClient, a client for ext.flutter.viewMetricsOverride, and connects the text scale, bold text, screen reader and high contrast controls of the Accessibility page to it.
There was a problem hiding this comment.
Code Review
This pull request implements support for the ext.flutter.viewMetricsOverride service extension, enabling users to override accessibility settings (text scale, bold text, screen reader, and high contrast) in debug builds. It introduces ViewMetricsOverrideClient to manage the service extension, handles state restoration across hot restarts, and updates the accessibility controller and UI overrides pane. Feedback focuses on improving error handling: rethrowing and catching service extension failures to notify users, handling background errors with .catchError, adding a defensive check against division by zero in _textScaleFactor, and using a try-finally block to ensure the text scale slider resets properly on failure.
| Future<void> _setOverride(String metric, Object? value) async { | ||
| await _viewMetricsOverrideClient?.updateOverrides({metric: value}); | ||
| } |
There was a problem hiding this comment.
[CONCERN] Catching the exception here and pushing a notification via notificationService provides a much better user experience. It ensures that if the service extension call fails, the user is notified immediately rather than the UI silently failing to apply the override.
| Future<void> _setOverride(String metric, Object? value) async { | |
| await _viewMetricsOverrideClient?.updateOverrides({metric: value}); | |
| } | |
| Future<void> _setOverride(String metric, Object? value) async { | |
| try { | |
| await _viewMetricsOverrideClient?.updateOverrides({metric: value}); | |
| } catch (e) { | |
| notificationService.push('Failed to set override: $e'); | |
| } | |
| } |
References
- Prioritize logic, performance on the UI thread, and architectural consistency. (link)
| static double? _textScaleFactor(Object? textScaling) { | ||
| if (textScaling case [[final num unscaled, final num scaled], ...]) { | ||
| return scaled / unscaled; | ||
| } | ||
| return null; | ||
| } |
There was a problem hiding this comment.
[CONCERN] Defensively check if unscaled is zero to avoid division by zero, which would result in double.infinity or double.nan and potentially cause layout or rendering issues in the UI.
| static double? _textScaleFactor(Object? textScaling) { | |
| if (textScaling case [[final num unscaled, final num scaled], ...]) { | |
| return scaled / unscaled; | |
| } | |
| return null; | |
| } | |
| static double? _textScaleFactor(Object? textScaling) { | |
| if (textScaling case [[final num unscaled, final num scaled], ...]) { | |
| return unscaled == 0 ? null : scaled / unscaled; | |
| } | |
| return null; | |
| } |
References
- Prioritize logic, performance on the UI thread, and architectural consistency. (link)
| Future<void> _overrideTextScale(double textScale) async { | ||
| await widget.controller.setTextScale(textScale); | ||
| if (mounted && _sliderTextScale == textScale) { | ||
| setState(() => _sliderTextScale = null); | ||
| } | ||
| } |
There was a problem hiding this comment.
[CONCERN] Wrapping the call in a try-finally block ensures that _sliderTextScale is reset to null even if the service call fails. This prevents the slider from getting stuck at an invalid/failed value and allows it to snap back to the actual value reported by the app.
Future<void> _overrideTextScale(double textScale) async {
try {
await widget.controller.setTextScale(textScale);
} finally {
if (mounted && _sliderTextScale == textScale) {
setState(() => _sliderTextScale = null);
}
}
}References
- Prioritize logic, performance on the UI thread, and architectural consistency. (link)
A call that throws no longer keeps the calls after it from running, and the future of a call that a hot restart interrupts now completes. A metric that DevTools stops overriding is no longer set again after a hot restart, and a restore that fails is tried again after the next one. The page ignores text scaling tables without a valid first point, and setTextScale asserts that its factor is finite and not negative. The text scale label now reads "System Default", like the brightness dropdown next to it.
After a hot restart, the client now restores the text scale, bold text, screen reader and high contrast overrides that the app had, whoever set them, rather than only the ones that this client set. So the page shows the same overrides after the restart, also after DevTools reloads. The fake extension parses writes with DebugViewMetricsOverride.fromJson, like the app, and can hold calls until a test releases them, so the tests of a hot restart during a call no longer poll. The client docs also say to create one client per connection.
The client no longer restores the overrides after a hot restart, which empties them, so the page shows them as off afterwards. Restoring them, along with brightness, is left to a follow-up. Also removes the Future.any that completed calls interrupted by a hot restart, shortens the docs of private members, and simplifies the held calls of the fake extension, which no test releases anymore.
The VM service never answers a call that is in flight when its isolate exits, so the future that updateOverrides returned for such a call never completed. The text scale slider shows the value it was dragged to until that future completes, so after a hot restart it could keep showing a text scale that the app no longer had. These futures now complete when the main isolate changes, and when the client is disposed. Also tests that the client reads the overrides of the new isolate after a hot restart.
The switch is meant to show the semantics debugger, not to override accessibleNavigation, which only changes how some widgets behave. Flutter has no service extension for the semantics debugger yet, so the switch goes back to the placeholder it was before this PR. It now sits below the controls that the extension backs, so that their "unavailable" tooltip does not cover it.
After a hot restart the client kept the overrides of the isolate that exited, so the page showed them until the new isolate answered its read, with the controls enabled if the app sat at a breakpoint by then or the read failed. The client now empties its overrides when the main isolate changes. The test of the read after a hot restart now fills the registry of the new isolate, because an empty one no longer shows that the client read it. Also documents that a write removes a change that app code made with debugSetViewMetricsOverride, which the app does not report, and tests that dispose completes the calls that the app has not answered.
A switch shows what the app reports, so while the app sat at a breakpoint a tap looked ignored, and a second tap sent the same write again. Like the text scale slider, a switch now shows its new value until the write is done. Also names an app that has not started yet in the tooltip of the disabled controls, which also shows during a hot restart.
Part of #9893.
This connects the text scale, bold text and high contrast overrides on the Accessibility page to the app. They go through
ext.flutter.viewMetricsOverride, which flutter/flutter#192764 added and flutter/flutter#194311 made take effect in the app.The new
ViewMetricsOverrideClient:Flutter.ServiceExtensionStateChangedevents. It listens to the events itself, becauseServiceExtensionManagerwould turn their JSON value into a string.On the page:
textScaling: [[1, factor]], which scales text linearly. Until text scale is overridden, the page shows "System Default", because 1.0 is an override too. A reset button removes the override.boldTextandhighContrast. Turning one off removes the override, so the app goes back to the platform's setting.Caveats:
debugSetViewMetricsOverride, which the app does not report. It also removes a change that another client made after DevTools last heard from the app, and before the write arrived.accessibilityexperiment flag.The client tests use a fake of the extension that parses writes like the app and, like the VM service, never answers a call that is in flight when its isolate exits. They cover following the app, failures, hot restarts, and calls that a hot restart or dispose interrupts. Controller and widget tests cover what the controls send and show. The
devtools_testfakes can now emit extension events and replace the main isolate.After this PR:
Pre-launch Checklist
General checklist
///).Issues checklist
contributions-welcomeorgood-first-issuelabel.contributions-welcomeorgood-first-issuelabel. I understand this means my PR might take longer to be reviewed.Tests checklist
AI-tooling checklist
Feature-change checklist
release-notes-not-requiredlabel or left a comment requesting the label be added.packages/devtools_app/release_notes/NEXT_RELEASE_NOTES.md.If you need help, consider asking for help on Discord.